using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    //凋落物逻辑
    public class DroppedItemsBase : MonoBehaviour
    {
        //掉落物数据文件
        [field: SerializeField, Tooltip("掉落物数据文件")]
        private DroppedItemsListSO droppedItemsListSO;

        //图标
        [field: SerializeField, Tooltip("图标")]
        private SpriteRenderer spriteRenderer;
        //当前掉落物品类型
        private GameObjEnumClass.DroppedItemsEnum _droppedTypeEnum;


        //定时器
        private float _timer = 0;
        //是否可以拾取
        private bool _isPickup = false;
        void Start()
        {
        }

        void Update()
        {
            _timer += Time.deltaTime;
        }

        //碰撞触发
        void OnTriggerEnter2D(Collider2D collider)
        {
            if (collider.CompareTag(PlayerStateReusableData.Collider2DTagEnum.Player.ToString()))
            {
                //玩家碰撞
                _isPickup = true;
            }
        }
        void OnTriggerExit2D(Collider2D collider)
        {
            if (collider.CompareTag(PlayerStateReusableData.Collider2DTagEnum.Player.ToString()))
            {
                //玩家碰撞
                _isPickup = false;
            }
        }

        #region Main function
        //设置位置
        public void MoveTransfromTo(Vector2 agentPos, Vector2 targetPos)
        {
            transform.position = new Vector3(agentPos.x, agentPos.y, transform.position.z);
            //协程进行移动
            StartCoroutine(MoveToTargetPos(new Vector2(agentPos.x + targetPos.x, agentPos.y + targetPos.y)));
        }
        //生成掉落物图标
        public void GenerateDroppedItems(GameObjEnumClass.DroppedItemsEnum droppedItemsEnum)
        {
            _droppedTypeEnum = droppedItemsEnum;
            List<DroppedItemData> dropedSpriteList = droppedItemsListSO.droppedItemsList;
            foreach (DroppedItemData item in dropedSpriteList)
            {
                if (item.DroppedItemsEnum == droppedItemsEnum)
                {
                    spriteRenderer.sprite = item.sprite;
                    return;
                }
            }
        }
        // 是否可以拾取
        public bool IsPickup()
        {
            return _isPickup;
        }

        //销毁
        public void DestroyDroppedItems()
        {
            _isPickup = false;
            Destroy(gameObject);
        }

        //向某一点移动
        private IEnumerator MoveToTargetPos(Vector2 targetPos)
        {
            float elapsed = 0f;
            float t = 0.5f;
            while (elapsed < t)
            {
                transform.position = Vector3.Lerp(transform.position, new Vector3(targetPos.x, targetPos.y, transform.position.z), 0.03f);
                elapsed += Time.deltaTime;
                yield return null;
            }
        }
        //获取掉落物品类型
        public GameObjEnumClass.DroppedItemsEnum GetDroppedItemsType()
        {
            return _droppedTypeEnum;
        }
        #endregion

    }
}
